package com.example.springbootrocketmq.transaction;

import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @Auther: 郭敬仰
 * @Date: 2019/8/24 23:25
 * @Description:
 */
@Component
public class SpringTransactionProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    /**
     * 发送消息
     *
     * @param topic
     * @param msg
     */
    public void sendMsg(String topic, String msg) {
        Message message = MessageBuilder.withPayload(msg).build();
      // myTransactionGroup要和@RocketMQTransactionListener(txProducerGroup ="myTransactionGroup")定义的一致
        TransactionSendResult myTransactionGroup = rocketMQTemplate.sendMessageInTransaction("myTransactionGroup", topic, message, null);
        System.out.println("发送消息成功");
    }
}